var addItem = function (id) {

    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        data: {
            func: 'addItem',
            item: id
        }
    });

    $(".ajx-loader").fadeIn();

    request.success(function (response) {

        $(".ajx-loader").fadeOut();

        var msg = jQuery.parseJSON(response);

        updateFrontValues.updateHeaderCart(msg.cartItems, msg.totalPrice);

        $.ambiance({message: msg.added.itemname + " added to your shopping cart!",
            type: "success"});
    });
}

var dcrcartItem = function (id) {

    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        async: false,
        data: {
            func: "dcrcartItem",
            id: id
        }
    });

    $(".ajx-loader").fadeIn();

    request.success(function (response) {

        $(".ajx-loader").fadeOut();

        var msg = jQuery.parseJSON(response);

        if (msg.totalPrice == null) {
            msg.totalPrice = 0;
        }

        updateFrontValues.updateHeaderCart(msg.cartItems, msg.totalPrice);
        updateFrontValues.updateCheckoutCart(msg.cart, msg.totalPrice);

        if (msg.cartItems == null) {
            window.location.replace("http://jcardell-asix.hol.es/");
        }

    });
}

var ncrcartItem = function (id) {

    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        async: false,
        data: {
            func: "ncrcartItem",
            id: id
        }
    });

    $(".ajx-loader").fadeIn();

    request.success(function (response) {

        $(".ajx-loader").fadeOut();

        var msg = jQuery.parseJSON(response);

        updateFrontValues.updateHeaderCart(msg.cartItems, msg.totalPrice);
        updateFrontValues.updateCheckoutCart(msg.cart, msg.totalPrice);
    });
}

var removeItem = function (id) {

    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        async: false,
        data: {
            func: "removeItem",
            id: id
        }
    });

    $(".ajx-loader").fadeIn();

    request.success(function (response) {

        $(".ajx-loader").fadeOut();

        var msg = jQuery.parseJSON(response);

        if (msg.totalPrice == null) {
            msg.totalPrice = 0;
        }

        updateFrontValues.updateHeaderCart(msg.cartItems, msg.totalPrice);
        updateFrontValues.updateCheckoutCart(msg.cart, msg.totalPrice);

        if (msg.cartItems == null) {
            window.location.replace("http://jcardell-asix.hol.es/");
        }
    });
}

var checkEmail = function () {
    var email = $("#email").val();

    if (email == "") {
        $.ambiance({message: "Without the email address we can't send you the product key!",
            type: "error"});

        return false;
    }
    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        async: true,
        data: {
            func: 'saveEmail',
            email: email
        }
    });

    $(".ajx-loader").fadeIn();

    request.success(function (response) {
        $(".ajx-loader").fadeOut();
        if (response != false) {
            window.location.replace(response);
        }
    });
}

var searchGame = function (game) {

    var request = $.ajax({

        url: "ajaxHandler.php",
        type: "POST",
        async: true,
        data: {
            func: 'searchGame',
            game: game
        }
    });

    request.success(function (response) {
        //var msg = jQuery.parseJSON(response);

        $("#items").empty();
        $("#items").append(response);
    });
}

//Class to update frontend values called in ajax functions
var updateFrontValues = {

    updateHeaderCart: function (items, price) {
        $(".cart .items").empty();
        $(".cart .items").append(items);
        $(".cart .total").empty();
        $(".cart .total").append(price + ' €');
    },

    updateCheckoutCart: function (cart, price) {
        $("table").empty();
        $("table").append(cart);
        $(".checkout-total strong").empty();
        $(".checkout-total strong").append(price + " €");
    }
};